<link rel="stylesheet" href="__PUBLIC__/css/jquery.treeview.css" />
<link rel="stylesheet" href="__PUBLIC__/css/jquery.screen.css" />
<script src="__PUBLIC__/js/jquery.treeview.js" type="text/javascript"></script>
<script src="__PUBLIC__/js/jquery.cookie.js" type="text/javascript"></script>
<section id="content">

    <div class="page page-fullwidth-layout">

        <div class="pageheader">

            <h2>{$data.module_name}</h2>

            <div class="page-bar">

                <ul class="page-breadcrumb">
                    <li>
                        <a href="{:url('/admin')}"><i class="fa fa-home"></i> 起始页</a>
                    </li>
                    <li>
                        权限列表
                    </li>
                </ul>

            </div>

        </div>

        <!-- page content -->
        <div class="pagecontent">

            <!-- row -->
            <div class="row">
                <!-- col -->
                <div class="col-md-12">




                    <!-- tile -->
                    <section class="tile">

                        <!-- tile header -->
                        <div class="tile-header dvd dvd-btm">
                            <h1 class="custom-font"><strong>权限</strong> 列表</h1>
                            <ul class="controls">
                                <!--<li><a href="{:url($data.module_url.'create')}"><i class="fa fa-plus mr-5"></i> 新增</a></li>-->
                            </ul>
                        </div>
                        <!-- /tile header -->

                        <!-- tile body -->
                        <div class="tile-body">
                            <div class="alert alert-info" style="display:none;">显示一些提示信息</div>
                            <div id="role_select_box">
                                <select name="group_select" style="display: inline-block;width:20%;" class="form-control">
                                    <option value="0">选择部门</option>
                                    {if($groups)}
                                    {foreach $groups as $key=>$v)}
                                    <option value="{$v['id']}">{$v['group_name']}</option>
                                    {/foreach}
                                    {/if}
                                </select>
                                <select name="role_select" style="display: inline-block;width:20%;" class="form-control">
                                    <option value="0">选择角色</option>
                                    {if($roles)}
                                    {foreach $roles as $key=>$v)}
                                    <option value="{$v['role_id']}">{$v['name']}</option>
                                    {/foreach}
                                    {/if}
                                </select>
                                <button class="btn btn-primary sub_permission">保存</button>
                            </div>

                            <ul class="treeview" id="tree">
                                <li class="collapsable lastCollapsable">
                                    <div class="hitarea collapsable-hitarea lastCollapsable-hitarea"></div>
                                    <span>节点目录</span>
                                    <ul style="display: block;">
                                        {if($permission)}
                                        {foreach $permission as $key=>$v)}
                                        <li class="collapsable">
                                            <div class="hitarea collapsable-hitarea"></div>
                                            <input type="checkbox" class="level_1" name="plist" value="{$v['id']}">{$v['name']}
                                            {if isset($v['child']) && count($v['child']) > 0}
                                            <ul style="display: block;">
                                                {foreach $v['child'] as $vo_item)}
                                                <li>
                                                    <input type="checkbox" class="level_2" name="plist" value="{$vo_item['id']}">{$vo_item['name']}
                                                </li>
                                                {/foreach}
                                            </ul>
                                            {/if}
                                        </li>
                                        {/foreach}
                                        {/if}
                                    </ul>
                                </li>
                            </ul>
                            <div class="clear"></div>

                        </div>
                        <!-- /tile body -->

                    </section>
                    <!-- /tile -->

                </div>
                <!-- /col -->
            </div>
            <!-- /row -->




        </div>
        <!-- /page content -->
        <script type="text/javascript">
            $(function() {
                $("#tree").treeview({
                    collapsed:false,
                    animated: "fast",
                    control: "#sidetreecontrol",
                    persist: "location"   //cookie刷新页面保持原状；location不保持
                });

                //选择部门触发选择角色
                $('select[name = group_select]').change(function(){
                    $('select[name=role_select ]').html('');
                    var group_id = $(this).val();
                    if(group_id > 0) {
                        $.ajax({
                            type:"POST",
                            url:"{:url('/admin/Users/getRolesByGroupId')}",
                            data: {group_id:group_id},
                            dataType: 'json',
                            success:function(res){
                                if(res.code == 200){
                                    if(res.data.length > 0){
                                        var i;
                                        var str = '<option value="0">选择角色</option>';
                                        for(i=0;i<res.data.length;i++){
                                            str += '<option value="'+res.data[i].role_id+'">'+res.data[i].name+'</option>';
                                        }
                                        // alert(str);
                                        $('select[name=role_select ]').html(str);
                                    }else{
                                        $('select[name=role_select ]').html('<option value="0">选择角色</option>');
                                    }
                                }
                            }
                        });
                    }
                })

                //选择角色获取角色权限
                $('select[name = role_select]').change(function(){
                    var group_id = $('select[name=group_select]').val();
                    var role_id  = $(this).val();
                    if(group_id > 0 && role_id > 0) {
                        window.location.href="/admin/Permission/permission_list/"+group_id+'/'+role_id;
                    }
                })

                //复选框（选中一级菜单，子菜单全选）
                $('input.level_1').change(function(){
                    if($(this).is(':checked')) {
                        $(this).next('ul').find('input.level_2').prop('checked',true);
                    }else{
                        $(this).next('ul').find('input.level_2').removeAttr('checked');
                    }
                })

                //复选框（选中二级菜单，母菜单选中）
                $('input.level_2').change(function(){
                    var pid = $(this).parents('ul').prev('input.level_1');
                    if($(this).is(':checked')) {
                        $(pid).prop('checked',true);
                    }else{
                        var check_flag = false;
                        $.each($(pid).next('ul').find('input:checkbox:checked'),function(){
                            check_flag = true;
                        });

                        if(check_flag == false){
                            $(this).parents('ul').prev('input.level_1').removeAttr('checked');
                        }
                    }
                })

                //点击提交
                $('.sub_permission').click(function(){
                    var group_id = $('select[name=group_select]').val();
                    var role_id  = $('select[name=role_select]').val();
                    if(group_id > 0 && role_id > 0){
                        //获取所有选中的checkbox
                        var check_ids = '';
                        $("input[name=plist]:checked").each(function(i){
                            check_ids += ($(this).val())+',';
                        });
                        check_ids=check_ids.substring(0,check_ids.length-1);
                        if(check_ids.length > 0){
                            $.ajax({
                                type:"POST",
                                url:"{:url('/admin/Permission/permission_add')}",
                                data: {group_id:group_id,role_id:role_id,check_ids:check_ids},
                                dataType: 'json',
                                success:function(res){
                                    if(res.code == 200){
                                        alert(res.message);return false;
                                    }
                                }
                            });
                        }else{
                            alert('请选择权限');return false;
                        }
                    }else{
                        alert('请选择部门及角色');return false;
                    }

                });

                /**
                 * 初始化页面属性
                 */
                //选择角色的权限（选中）
                $('select[name = group_select]').val('<?=$group_id?>');
                $('select[name = role_select]').val('<?=$role_id?>');

                //选择角色获取角色当前拥有的权限
                var permision_arr = [];
                permision_arr = '<?= $role_permission ?>'.split(",");
                $("input[name='plist']").each(function(){
                    var vl = $(this).val();
                    for(var i in permision_arr){
                        if(permision_arr[i] == vl){
                            $(this).attr('checked', 'checked');
                        }
                    }
                });
            })
        </script>

    </div>

</section>
<!--/ CONTENT -->

